package com.elong.qadev.service.impl;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elong.qadev.dao.CaseDaoI;
import com.elong.qadev.dao.HttpCaseDaoI;
import com.elong.qadev.dao.TaskDaoI;
import com.elong.qadev.dao.WebuiCaseDaoI;
import com.elong.qadev.entity.CaseNode;
import com.elong.qadev.entity.HttpCase;
import com.elong.qadev.entity.TaskInfo;
import com.elong.qadev.entity.WebuiCase;
import com.elong.qadev.model.CaseInfo;
import com.elong.qadev.service.TaskServiceI;

@Service("taskService")
public class TaskServiceImpl implements TaskServiceI {
	@Autowired
	public HttpCaseDaoI httpCase;
	@Autowired
	public CaseDaoI caseDao;
	@Autowired
	public TaskDaoI	taskDao;
	@Autowired
	public WebuiCaseDaoI webuiCaseDao;
	@Override
	public ArrayList<CaseInfo> getHttpCaseInfo() {
		ArrayList<CaseInfo> ac = new ArrayList<CaseInfo>();
		ArrayList<CaseNode> an = new ArrayList<CaseNode>();
		an = caseDao.getAllSubCases();
		for (int i = 0; i < an.size(); i++) {
			CaseInfo ci = new CaseInfo();
			CaseNode cn = an.get(i);
			if(cn.getCaseType().equalsIgnoreCase("HTTP")){
				HttpCase hc = httpCase.getHttpCase(cn.getId());
				ci.setId(cn.getId());
				ci.setLevel("P1");
				ci.setName(cn.getName());
				ci.setType(cn.getCaseType());
				if(hc!=null){
					ci.setCreateDate(hc.getCreateDateTime());
					ci.setOwner(hc.getOwer());
				}				
			}else if(cn.getCaseType().equalsIgnoreCase("WEBUI")){
				WebuiCase wc = webuiCaseDao.getWebuiCase(cn.getId());
				ci.setId(cn.getId());
				ci.setLevel("P1");
				ci.setName(cn.getName());
				ci.setType(cn.getCaseType());
				if(wc!=null){
					ci.setCreateDate(wc.getCreateDateTime());
					ci.setOwner(wc.getOwner());
				}
			}
			ac.add(ci);
		}
		return ac;
	}

	@Override
	public ArrayList<TaskInfo> getTaskInfo() {
		return taskDao.getAllTaskInfo();
	}

	@Override
	public boolean saveTaskInfo(TaskInfo ti) {
		return taskDao.addTask(ti);
	}

	@Override
	public boolean deleteTaskInfo(int id) {
		return taskDao.deleteTask(id);
	}

	@Override
	public boolean updateTaskInfo(TaskInfo ti) {
		// TODO Auto-generated method stub
		return taskDao.upupdateTaskInfo(ti);
	}

	@Override
	public TaskInfo getTaskInfoById(int id) {
		
		return taskDao.getTaskInfoById(id);
	}

}
